package com.qijian.maindata.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qijian.common.utils.ObjectUtils;
import com.qijian.maindata.domain.BomCraftFile;
import com.qijian.maindata.domain.dto.BomStorageDTO;
import com.qijian.maindata.domain.query.BomCraftFileQuery;
import com.qijian.maindata.domain.vo.BomCraftFileVo;
import com.qijian.maindata.mapper.BomCraftFileMapper;
import com.qijian.maindata.service.IBomCraftFileService;
import com.qijian.tool.domain.Storage;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author qijian
 * @date 2023-11-15
 */
@Service
@RequiredArgsConstructor
public class BomCraftFileServiceImpl extends ServiceImpl<BomCraftFileMapper, BomCraftFile> implements IBomCraftFileService {

    private final BomCraftFileMapper bomCraftFileMapper;

    @Override
    public Page<BomCraftFileVo> pageVo(Page<BomCraftFile> page, Wrapper<BomCraftFileQuery> queryWrapper) {
        Page<BomCraftFileVo> voPage = bomCraftFileMapper.listBomCraftFile(page, queryWrapper);
        List<BomCraftFileVo> records = voPage.getRecords();
        for (BomCraftFileVo record : records) {
            List<BomCraftFileVo> historyList = record.getHistoryList();
            if (CollUtil.isNotEmpty(historyList)) {
                for (BomCraftFileVo bomCraftFileVo : historyList) {
                    bomCraftFileVo.setItemCode(record.getItemCode());
                    bomCraftFileVo.setItemName(record.getItemName());
                }
            }

        }
        return voPage;
    }

    /**
     * 新增或者替换Bom工艺文件
     *
     * @param dto
     */
    @Override
    public void bomStorageReplace(BomStorageDTO dto) {
        List<Storage> files = dto.getFiles();
        // 将被替换的工艺文件的isEnable状态置为o
        if (ObjectUtils.isNotNull(dto.getOldId())) {
            BomCraftFile oldCraftFile = this.getById(dto.getOldId());
            Long version = oldCraftFile.getVersion();
            this.lambdaUpdate().eq(BomCraftFile::getId, dto.getOldId()).set(BomCraftFile::getIsEnable, false).update();
            for (Storage file : files) {
                BomCraftFile bomCraftFile = new BomCraftFile();
                bomCraftFile.setBomId(dto.getBomId());
                bomCraftFile.setProjectId(dto.getProjectId());
                bomCraftFile.setProjectMachineNo(dto.getProjectMachineNo());
                bomCraftFile.setStorageId(file.getStorageId());
                bomCraftFile.setVersion(version);
                bomCraftFile.setRemark(dto.getRemark());
                this.save(bomCraftFile);
            }
        } else {
            for (Storage file : files) {
                BomCraftFile bomCraftFile = new BomCraftFile();
                bomCraftFile.setBomId(dto.getBomId());
                bomCraftFile.setStorageId(file.getStorageId());
                this.save(bomCraftFile);
                this.lambdaUpdate().eq(BomCraftFile::getId, bomCraftFile.getId()).set(BomCraftFile::getVersion, bomCraftFile.getId()).update();
            }
        }


    }

    /**
     * 统计 bom的工艺文件数量
     *
     * @param bomIds bomId数组
     * @return Map<bomId, 工艺文件数量>
     */
    @Override
    public Map<Long, Long> countBomCraftSize(List<Long> bomIds) {
        Map<Long, Long> result = new HashMap<>();
        List<BomCraftFile> craftFileList = this.lambdaQuery().in(BomCraftFile::getBomId, bomIds).eq(BomCraftFile::getIsEnable, true).list();
        if (CollUtil.isNotEmpty(craftFileList)) {
            result = craftFileList.stream().collect(Collectors.groupingBy(BomCraftFile::getBomId, Collectors.counting()));
        }
        return result;
    }
}
